var seckill = {
    URL: {
        now: '/seckill/time/now'
        ,
        export: function (seckillId) {
            return '/seckill/' + seckillId + '/exprot';
        },
        execution: function (seckillId, md5) {
            return '/seckill/' + seckillId + '/' + md5 + '/execution';
        }

    },
    handlerSeckill: function (seckillId, node) {
        //处理秒杀逻辑
        node.hide().html('<button class="btn btn-primary btn-lg" id="killbtn" >开始秒杀</button>');
        $.ajax({
            url: seckill.URL.export(seckillId),
            type: "post",
            data: {},
            success: function (result) {
                if (result && result.success) {
                    var exp = result.data;
                    if (exp.success) {
                        //开启秒杀
                        var killurl = seckill.URL.execution(seckillId, exp.md5);
                        //绑定一次事件
                        $("#killbtn").one("click", function () {
                            //执行秒杀
                            //禁用按钮
                            $(this).addClass('disabled');
                            $.post(killurl, {}, function (result) {
                                if(result && result.success){
                                    var info = result.data.info;

                                    node.html('<span class="label label-success">' + info + '</span>');
                                }else {
                                    var msg = result.desc;
                                    node.html('<span class="label label-danger">' + msg + '</span>');
                                }


                            });
                        });
                        node.show();

                    } else {
                        var now = result.now;
                        var start = result.start;
                        var end = result.end;
                        seckill.countDown(seckillId, now, start, end);
                    }
                }
            }
        })
    },
    checkPhone: function (phone) {
        if (phone && phone.length == 11 && !isNaN(phone)) {
            return true;
        } else {
            return false;
        }
    },
    countDown: function (seckillId, now, startTime, endTime) {
        var box = $("#seckill-box");
        if (now > endTime) {
            box.html('秒杀结束');
        } else if (now < startTime) {

            var killtime = new Date(startTime + 1000);
            box.countdown(killtime, function (event) {
                var fmt = event.strftime('秒杀倒计时:%D天 %H时 %M分 %S秒');
                box.html(fmt);
            }).on('finish.countdown', function () {
                //开始秒杀
                seckill.handlerSeckill(seckillId, box);
            });
        } else {
            seckill.handlerSeckill(seckillId, box);

        }
    },
    detail: {
        init: function (params) {
            var phone = $.cookie('phone');
            var startTime = params['startTime'];
            var endTime = params['endTime'];
            var seckillId = params['seckillId'];
            if (!seckill.checkPhone(phone)) {
                $("#seckillModel").modal({
                    show: true,//线束
                    backdrop: 'static',//禁止位置关闭
                    keyboard: false//关闭键盘事件
                });
                $("#seckillModalBtn").click(function () {
                    var input = $("#phone").val();
                    if (seckill.checkPhone(input)) {
                        $.cookie('phone', input, {expires: 7, path: '/seckill'});
                        window.location.reload();
                    } else {
                        $("#message").hide().html('<label class="label label-danger">输入手机号错误</label>>').show(300);

                    }
                });
            }


            $.get(seckill.URL.now, {}, function (result) {
                var d = JSON.parse(result);
                if (d && d['success']) {
                    var now = d['data'];
                    seckill.countDown(seckillId, now, startTime, endTime);
                }
            });

        }

    }

}